<html>
<head><meta charset="utf-8"><title>Distinguishing enums from other types at compile time · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Distinguishing.20enums.20from.20other.20types.20at.20compile.20time.html">Distinguishing enums from other types at compile time</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="204321951"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Distinguishing%20enums%20from%20other%20types%20at%20compile%20time/near/204321951" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brendan Molloy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Distinguishing.20enums.20from.20other.20types.20at.20compile.20time.html#204321951">(Jul 18 2020 at 23:09)</a>:</h4>
<p>Is there a mechanism to distinguish whether a type is an enum and that can be statically asserted? If not statically asserted, then non-statically is also acceptable.</p>



<a name="204322510"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Distinguishing%20enums%20from%20other%20types%20at%20compile%20time/near/204322510" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Distinguishing.20enums.20from.20other.20types.20at.20compile.20time.html#204322510">(Jul 18 2020 at 23:29)</a>:</h4>
<p>You want the equivalent of <code>where T: IsEnum</code> in regular Rust code? Or in the compiler?</p>
<p>If the former, not AFAIK. Perhaps if you share why you need this, someone might have an alternative for you.</p>



<a name="204322555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Distinguishing%20enums%20from%20other%20types%20at%20compile%20time/near/204322555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Distinguishing.20enums.20from.20other.20types.20at.20compile.20time.html#204322555">(Jul 18 2020 at 23:30)</a>:</h4>
<p>I don’t think you can abuse <a href="https://doc.rust-lang.org/std/mem/fn.discriminant.html">https://doc.rust-lang.org/std/mem/fn.discriminant.html</a> for this either.</p>



<a name="204322712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Distinguishing%20enums%20from%20other%20types%20at%20compile%20time/near/204322712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brendan Molloy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Distinguishing.20enums.20from.20other.20types.20at.20compile.20time.html#204322712">(Jul 18 2020 at 23:36)</a>:</h4>
<p>Yes, it cannot be abused even unsafely because 0 is the result for things that aren't enums and some valid cases for enums. I'm experimenting with garbage collection in Rust, and one situation I'm considering is manipulating enums based on their discriminator, so I would like to special case enums. Specialisation would work but isn't going to be stabilised any time soon so not realistic, same goes for auto traits. I can already have the enums be opt-in with a special trait but then comes other complexity.</p>



<a name="204322739"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Distinguishing%20enums%20from%20other%20types%20at%20compile%20time/near/204322739" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brendan Molloy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Distinguishing.20enums.20from.20other.20types.20at.20compile.20time.html#204322739">(Jul 18 2020 at 23:37)</a>:</h4>
<p>so right now I do have a <code>TaggedUnion</code> trait being used for manipulating enums that have opted in, with <code>#[repr(C, uXXX)]</code> so tags and discriminants can be accessed "safely"</p>



<a name="204322781"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Distinguishing%20enums%20from%20other%20types%20at%20compile%20time/near/204322781" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brendan Molloy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Distinguishing.20enums.20from.20other.20types.20at.20compile.20time.html#204322781">(Jul 18 2020 at 23:38)</a>:</h4>
<p>given that any type that wishes to function in my GC model requires a <code>Trace</code> trait to be implemented anyway, I could just have something like <code>fn is_enum() -&gt; bool { false }</code> and have the enums opt into it but it feels ... not nice</p>



<a name="204323731"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Distinguishing%20enums%20from%20other%20types%20at%20compile%20time/near/204323731" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Brendan Molloy <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Distinguishing.20enums.20from.20other.20types.20at.20compile.20time.html#204323731">(Jul 19 2020 at 00:11)</a>:</h4>
<p>I realised I _can_ get away with just using <code>std::mem::discriminant</code>! Comparing equality for discriminants on any given T will return true always if it's not an enum, and if it's an enum, will return false on an equality check if the value has changed. Turns out that's all I actually need to solve my immediate problem, so I got lucky, haha</p>



<a name="204325982"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Distinguishing%20enums%20from%20other%20types%20at%20compile%20time/near/204325982" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Distinguishing.20enums.20from.20other.20types.20at.20compile.20time.html#204325982">(Jul 19 2020 at 01:21)</a>:</h4>
<p>Uhm, is it stable that <code>mem::discriminant</code> returns _the same_ value? The doc says the value of a discriminant can not change in the same compilation, is the same true for the indeterminate value of non-enum types?</p>



<a name="204337675"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Distinguishing%20enums%20from%20other%20types%20at%20compile%20time/near/204337675" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Distinguishing.20enums.20from.20other.20types.20at.20compile.20time.html#204337675">(Jul 19 2020 at 07:43)</a>:</h4>
<p><span class="user-mention silent" data-user-id="209548">Brendan Molloy</span> <a href="#narrow/stream/122651-general/topic/Distinguishing.20enums.20from.20other.20types.20at.20compile.20time/near/204323731">said</a>:</p>
<blockquote>
<p>I realised I _can_ get away with just using <code>std::mem::discriminant</code>! Comparing equality for discriminants on any given T will return true always if it's not an enum, and if it's an enum, will return false on an equality check if the value has changed. Turns out that's all I actually need to solve my immediate problem, so I got lucky, haha</p>
</blockquote>
<p>That's very thin ice you are walking on here. It certainly isn't true of generators (which might be fine, as they are just enums in disguise) and really is not guaranteed for any type</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>